/*
 * This file is part of b.AMP.
 *
 * b.AMP is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * b.AMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with b.AMP.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "addon.hpp"
#include "logger.hpp"

#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <string>
#include <vector>

namespace pt = boost::property_tree;
namespace fs = boost::filesystem;

void bamp::Addon::load(const boost::filesystem::path& dir)
{
    // Build the add-on descriptor filename
    fs::path file = dir / "addon.cfg";
    
    // Load the descriptor
    pt::ptree tree;
    read_xml(file.string(), tree);

    // Populate plugin lists
    BOOST_FOREACH(pt::ptree::value_type& v, tree)
    {
        // Put the plugin into the right plugin container.
        if (v.first == "plugin")
        {
            plugins.push_back(v.second.data());
        }
        else if (v.first == "extension")
        {
            Extension ext;
            BOOST_FOREACH(pt::ptree::value_type &v2, v.second)
            {
                if (v2.first == "exec")
                    ext.exec = v2.second.data();
                else if (v2.first == "arg")
                    ext.args.push_back(v2.second.data());
            }
        }
    }
}

void bamp::Addon::reset()
{
    plugins.clear();
    extensions.clear();
}
